package org.msfj.admin.org.service.impl;

import org.msfj.admin.model.User;
import org.msfj.admin.model.UserRole;
import org.msfj.admin.org.service.UserRoleService;
import org.msfj.admin.vo.UserRoleVo;
import org.msfj.admin.model.User;
import org.msfj.admin.model.UserRole;
import org.msfj.admin.org.service.UserRoleService;
import org.msfj.admin.vo.UserRoleVo;
import org.msfj.admin.model.User;
import org.msfj.admin.model.UserRole;
import org.msfj.admin.vo.UserRoleVo;
import org.msfj.admin.org.mapper.UserRoleMapper;
import org.springframework.stereotype.Service;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.enums.Enable;
import org.msfj.admin.org.service.UserRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 系统用户角色关联信息service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-10 17:30:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */

@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "userRole")
public class UserRoleServiceImpl extends BaseServiceImpl<UserRole> implements UserRoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 根据条件分页查询系统用户角色关联信息列表
     *
     * @param userRoleVo
     * @param page       当前第几页
     * @param rows       每页多少条记录
     * @return PageInfo<UserRole>
     */
    @Override
    //@Cacheable
    public PageInfo<UserRoleVo> getByUserRole(UserRoleVo userRoleVo, int page, int rows) {
        Example example = new Example(UserRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        PageHelper.startPage(page, rows);
        List<UserRole> userRoleList = super.getByExample(example);
        PageInfo userRoleVoPageInfo = new PageInfo(userRoleList);
        userRoleVoPageInfo.setList(setOtherProperties(userRoleList));
        return userRoleVoPageInfo;
    }

    /**
     * 根据条件查询所有系统用户角色关联信息对象列表
     *
     * @param userRoleVo
     * @return List<UserRole>
     */
    @Override
    //@Cacheable
    public List<UserRoleVo> getAllUserRole(UserRoleVo userRoleVo) {
        Example example = new Example(UserRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        if (ObjectUtil.isNotNull(userRoleVo.getRoleId())) {
            criteria.andEqualTo("roleId", userRoleVo.getRoleId());
        }
        List<UserRoleVo> userRoleVoList = setOtherProperties(super.getByExample(example));
        return userRoleVoList;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param userRole
     * @return DepartmentVo
     */
    @Override
    //@Cacheable
    public UserRoleVo setOtherProperties(UserRole userRole) {
        UserRoleVo userRoleVo = new UserRoleVo();
        BeanUtils.copyProperties(userRole, userRoleVo);
        return userRoleVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param userRoles
     * @return List<UserRoleVo>
     */
    @Override
    //@Cacheable
    public List<UserRoleVo> setOtherProperties(List<UserRole> userRoles) {
        List<UserRoleVo> userRoleVos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(userRoles)) {
            for (UserRole userRole : userRoles) {
                UserRoleVo userRoleVo = new UserRoleVo();
                BeanUtils.copyProperties(userRole, userRoleVo);
                userRoleVos.add(userRoleVo);
            }
        }
        return userRoleVos;
    }

    /**
     * 批量插入角色关联用户
     *
     * @param roleId
     * @param userIds
     * @param userId
     */
    @Override
    @Transactional
    public void saveListByUserRole(String roleId, List<String> userIds, String userId) {
        List<UserRole> userRoles = CollectionUtil.newArrayList();
        Example example = new Example(UserRole.class);
        example.createCriteria().andEqualTo("roleId", roleId);
        userRoleMapper.deleteByExample(example);
        if (CollectionUtil.isNotEmpty(userIds)) {
            for (String userId1 : userIds) {
                UserRole userRole = new UserRole();
                userRole.setRoleId(roleId);
                userRole.setUserId(userId1);
                userRole.setCreateBy(userId);
                userRole.setUpdateBy(userId);
                userRoles.add(userRole);
            }
            super.saveList(userRoles, User.class, UserRole.class);
        }
    }
}
